package com.lzy.business.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.DeptServiceApi;
import com.lzy.base.dtos.DeptDto;
import com.lzy.business.facade.ProductClassifyFacade;
import com.lzy.business.request.ProductClassifyRequest;
import com.lzy.business.response.ProductClassifyResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.ProductClassifyDeptServiceApi;
import com.lzy.product.apis.ProductClassifyServiceApi;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dtos.ProductClassifyDeptDto;
import com.lzy.product.dtos.ProductClassifyDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品分类控制类
 * Created by WIN7 on 2017/11/30.
 */

@Controller
@RequestMapping(value="/admin/product/classify")
public class ProductClassifyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductClassifyController.class);
    @Autowired
    ProductClassifyServiceApi productClassifyServiceApi;

    @Autowired
    ProductClassifyFacade productClassifyFacade;

    @Autowired
    ProductClassifyDeptServiceApi productClassifyDeptServiceApi;

    @Autowired
    DeptServiceApi deptServiceApi;

    /**
     * 产品分类列表页面
     * @return
     */
    @RequestMapping(value="",method = RequestMethod.GET)
    public String list(){
        return "product/classify_list";
    }

    /**
     * 查询列表
     * @param request
     * @return
     */
    @RequestMapping(value="/json",method = RequestMethod.GET)
    @ResponseBody
    public PageBean json(ProductClassifyRequest request){
        Ensure.that(request).isNotNull("01000003");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<ProductClassifyDto> pageInfo = productClassifyFacade.getClassifyList(pager, DozerUtils.map(request, ProductClassifyDto.class));
        if(null!=pageInfo){
            List<ProductClassifyDto> list = pageInfo.getList();
            return new PageBean<ProductClassifyResponse>(pageInfo.getTotal(), DozerUtils.maps(list, ProductClassifyResponse.class));
        }else{
            return null;
        }
    }

    /**
     * 新增分类页面
     */
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public String add(Model model){
        List<DeptDto> deptList= deptServiceApi.getAll(null);
        if(deptList != null && deptList.size() > 0) {
            model.addAttribute("deptList", deptList);
        }
        return "product/classify_add";
    }

    /**
     * 新增产品分类
     * @param request
     * @return
     */
    @RequestMapping(value="/save",method = RequestMethod.POST)
    @ResponseBody
    public Result save(ProductClassifyRequest request){
        Ensure.that(request).isNotNull("01000003");
        request.setCreateUser(UserUtil.getCurrentUser().getId());
        request.setSurplus(request.getCirculation());
        Boolean b = productClassifyFacade.addClassify(DozerUtils.map(request,ProductClassifyDto.class));
        Ensure.that(b).isTrue("02000001");
        return new Result();
    }

    /**
     * 编辑分类页面
     */
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public String edit(@RequestParam("id") Long id, Model model){
        Ensure.that(id).isNotNull("01000003");
        ProductClassifyDto pcDto = productClassifyFacade.getById(id);
        Ensure.that(pcDto).isNotNull("02000002");
        model.addAttribute("dto",pcDto);
        //获取分类与部门关联信息
        //部门deptIdList
        List<Long> deptIdList = new ArrayList<>();
        //部门名称deptNameList
        String nameList = "";
        ProductClassifyDeptDto pcdDto = new ProductClassifyDeptDto();
        pcdDto.setClassifyId(id);
        List<ProductClassifyDeptDto> productClassifyDeptDtoList = productClassifyDeptServiceApi.selectProductClassifyDept(pcdDto);
        if(productClassifyDeptDtoList != null && productClassifyDeptDtoList.size() > 0) {
            for (ProductClassifyDeptDto pDto : productClassifyDeptDtoList) {
                if (pDto != null && pDto.getDeptId() != null) {
                    deptIdList.add(pDto.getDeptId());
                    //获取部门名称
                    DeptDto deptDto = deptServiceApi.findDeptDtoById(pDto.getDeptId());
                    if (deptDto != null && deptDto.getDeptName() != null) {
                        nameList = nameList + deptDto.getDeptName() + " / ";
                    }
                }
            }
        }
        model.addAttribute("deptIdList",deptIdList);
        model.addAttribute("nameList",nameList.substring(0,nameList.length() - 3));
        //获取所有部门列表信息
        List<DeptDto> deptDtoList = deptServiceApi.getAll(null);
        if(deptDtoList != null && deptDtoList.size()>0){
            model.addAttribute("deptList",deptDtoList);
        }
        return "product/classify_add";
    }

    /**
     * 更新产品分类
     */
    @RequestMapping(value="/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(ProductClassifyRequest request){
        Ensure.that(request).isNotNull("01000003");
        request.setUpdateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productClassifyFacade.updateClassify(DozerUtils.map(request,ProductClassifyDto.class));
        Ensure.that(b).isTrue("02000004");
        return new Result();
    }

    /**
     * 查看产品分类页面
     */
    @RequestMapping(value="/view",method = RequestMethod.GET)
    public String detail(@RequestParam("id") Long id, Model model){
        Ensure.that(id).isNotNull("01000003");
        ProductClassifyDto pcDto = productClassifyFacade.selectById(id);
        Ensure.that(pcDto).isNotNull("02000002");
        model.addAttribute("dto",pcDto);
        return "product/classify_view";
    }

    /**
     * 移除
     */
    @RequestMapping(value="/remove",method = RequestMethod.POST)
    @ResponseBody
    public Result remove(@RequestParam("id") Long id){
        Ensure.that(id).isNotNull("01000003");
        Boolean b = productClassifyServiceApi.remove(id);
        Ensure.that(b).isTrue("02000007");
        return new Result();
    }

    /**
     * 批量移除
     */
    @RequestMapping(value="/removeList",method = RequestMethod.POST)
    @ResponseBody
    public Result removeList(@RequestParam("ids") Long[] idList){
        Ensure.that(idList).isNotNull("01000003");
        Boolean b = productClassifyFacade.removeList(idList);
        Ensure.that(b).isTrue("02000007");
        return new Result();
    }

}
